<?php

defined('SYSPATH') or die('No direct script access.');

/**
 * 更新活动商品状态
 * @version $Id: activity.php 11 2011-12-24 01:35:31Z 周永隆 $
 */
class CLI_Activity extends CLI
{
    protected $_run_time = 60; // 估计运行间隔

    public function main() {
        try{
            $bench_start = microtime(TRUE);

            $update_goods = array();

            // 团购、抢购、专题等不是秒杀 已开始但状态为未开始情况
            $select = DB::select('id', 'goods_id')
                ->from('activity_goods')
                ->where('actv_start_time', '<', time())
                ->where('actv_end_time', '>=', time())
                ->where('actv_status', '=', 1)
                ->where('actv_type', '<>', 3)
                ->fetch_all();
            foreach ($select as $v)
            {
                $update_goods[] = $v['goods_id'];
                DB::update('activity_goods')
                    ->set(array('actv_status' => 2))
                    ->where('id', '=', $v['id'])
                    ->execute();
            }

            // 秒杀已开始但状态为未开始情况
            $select = DB::select('id', 'goods_id')
                ->from('activity_goods')
                ->where('actv_start_time', '<', time())
                ->where('actv_start_time', '>=', time()+30)
                ->where('actv_status', '=', 1)
                ->where('actv_type', '=', 3)
                ->fetch_all();
            foreach ($select as $v)
            {
                $update_goods[] = $v['goods_id'];
                DB::update('activity_goods')
                    ->set(array('actv_status' => 2))
                    ->where('id', '=', $v['id'])
                    ->execute();
            }

            // 团购、抢购、专题等不是秒杀 已结束但状态为未开始或进行中情况
            $select = DB::select('id', 'goods_id')
                ->from('activity_goods')
                ->where('actv_end_time', '<', time())
                ->where('actv_status', 'IN', DB::expr('(1,2)'))
                ->where('actv_type', '<>', 3)
                ->fetch_all();
            foreach ($select as $v)
            {
                $update_goods[] = $v['goods_id'];
                DB::update('activity_goods')
                    ->set(array('actv_status' => 3))
                    ->where('id', '=', $v['id'])
                    ->execute();
            }

            // 秒杀已结束但状态为未开始或进行中情况
            $select = DB::select('id', 'goods_id')
                ->from('activity_goods')
                ->where('actv_start_time', '<', time()+30)
                ->where('actv_status', 'IN', DB::expr('(1,2)'))
                ->where('actv_type', '=', 3)
                ->fetch_all();
            foreach ($select as $v)
            {
                $update_goods[] = $v['goods_id'];
                DB::update('activity_goods')
                    ->set(array('actv_status' => 3))
                    ->where('id', '=', $v['id'])
                    ->execute();
            }

            // 更新goods表中activity_prop
            foreach ($update_goods as $goods_id)
            {
                $select = DB::select('actv_type', 'actv_id')
                    ->from('activity_goods')
                    ->where('goods_id', '=', $goods_id)
                    ->where('actv_status', 'IN', DB::expr("(1,2)"))
                    ->order_by('id', 'ASC')
                    ->fetch_all();

                $actvs = array();
                foreach ($select as $row)
                {
                    $actvs[$row['actv_type']] = array('actv_type' => $row['actv_type'],
                        'actv_id' => $row['actv_id']);
                }

                $actv_str = '';
                foreach ($actvs as $v)
                {
                    $actv_str .= $v['actv_type'].':'.$v['actv_id'].';';
                }
                $actv_str = trim($actv_str, ';');

                DB::update('goods')
                    ->set(array('activity_prop' => $actv_str, 'updated' => time()))
                    ->where('goods_id', '=', $goods_id)
                    ->execute();

                Detail::deleted_item_cache($goods_id);
            }

            // 把3个月前结束活动商品移至回收表
            $year = date('Y');
            $month = date('n');
            $day = date('d');
            $month -= 3;
            if ($month <= 0)
            {
                $month += 12;
                $year--;
            }

            $rs = DB::select('*')
                ->from('activity_goods')
                ->where('actv_start_time', '<', strtotime($year.'-'.$month.'-'.$day.' 00:00:00'))
                ->where('actv_status', '=', 3)
                ->fetch_all();
            foreach ($rs as $row)
            {
                DB::insert('activity_goods_bak', array_keys($row))
                    ->values($row)
                    ->execute();
                DB::delete('activity_goods')
                    ->where('id', '=', $row['id'])
                    ->execute();
            }

            $bench_end = microtime(TRUE);
            $eclipse_time = round($bench_end - $bench_start, 3);
            $log = "更新活动商品状态 执行用时  {$eclipse_time} 秒.".date('Y-m-d H:i:s');
            CLI::output("\n" . $log);
        }
        catch(Exception $e)
        {
            DB::delete('cli_running')
                ->where('class', '=', get_class($this))
                ->execute();
            CLI::output("\n" . $e->getMessage());
        }
    }

    /**
     * 构造方法
     *
     * @param  CLI_Args  $args
     * @return void
     */
    public function __construct(CLI_Args $args = NULL)
    {
        $this->args = $args;
        if(DB::select('addtime')
            ->from('cli_running')
            ->where('class', '=', get_class($this))
            ->where('addtime', '>', time() - $this->_run_time)
            ->fetch_one())
        {
            CLI::output("\n 此分析器正在运行中");
            die();
        }
        else
        {
            $this->_runed = true;
            DB::insert('cli_running', array('class', 'addtime'))
                ->values(array(get_class($this), time()))
                ->execute();
        }
    }

    /**
     * 删除运行状态
     */
    public function  __destruct()
    {
        if($this->_runed)
        {
            DB::delete('cli_running')->where('class', '=', get_class($this))->execute();
        }
    }

}